import { WasmLoader } from "@core/loader";
import type { CalculatorWasmExports } from "./types";

/**
 * 计算器类（业务胶水代码示例）
 */
export class Calculator {
  private loader: WasmLoader<CalculatorWasmExports>;
  constructor(wasmPath: string) {
    this.loader = new WasmLoader(wasmPath);
  }

  /**
   * 加法
   */
  async add(a: number, b: number): Promise<number> {
    return this.loader.call("add", a, b);
  }

  /**
   * 乘法
   */
  async multiply(a: number, b: number): Promise<number> {
    return this.loader.call("multiply", a, b);
  }

  /**
   * 预加载 WASM 模块
   */
  async preload(): Promise<void> {
    await this.loader.load();
  }
}

/**
 * 工厂函数：创建计算器实例
 */
export async function createCalculator(wasmPath: string): Promise<Calculator> {
  const calculator = new Calculator(wasmPath);
  await calculator.preload();
  return calculator;
}
